package com.api.gbhg.luoao.web;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.api.gbhg.luoao.la.util.DaoUtil;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通用项目调整
 */
@Path("/gbhg")
public class ProjectToAdjustInfoApi {
    /**
     * 项目调整基本数据查询
     */
    @POST
    @Path("/ProjectToAdjustInfo")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String GetProjectToAdjustInfo(Map<String,Object> params){
        String id = Util.null2String(params.get("id"));
        Map<String,Object> map = new HashMap<>();
        RecordSet recordSet = new RecordSet();
        String jinfei_versionSql = "select \n" +
                "lcbj,\n" +
                "xmmin,\n" +
                "lcjlsqjlid,\n" +
                "yszje,\n" +
                "ysnd,\n" +
                "lxdh,\n" +
                "(select lastname from HrmResource where id =jv.jbr) as jbrName,\n" +
                "(select lastname from HrmResource where id =jv.fzr) as fzrName,\n" +
                "(select subcompanyname from HrmSubCompany where id = jv.sbdw) as sbdw,\n" +
                "(select departmentname from HrmDepartment where id = jv.sbks) as sbks,\n" +
                "(select subcompanyname from hrmsubcompany where id = jv.dlhsdw) as dlhsdw,\n" +
                "jv.sydw\n" +
                "from uf_jingfei_version jv\n" +
                "where jv.lcjlsqjlid = ?";
        recordSet.executeQuery(jinfei_versionSql,id);
        Map<String,Object> versionMap = new HashMap<>();
        if (recordSet.next()){
            versionMap.put("lcbj",recordSet.getString("lcbj"));
            versionMap.put("xmmin",recordSet.getString("xmmin"));
            versionMap.put("lcjlsqjlid",recordSet.getString("lcjlsqjlid"));
            versionMap.put("yszje",recordSet.getString("yszje"));
            versionMap.put("ysnd",recordSet.getString("ysnd"));
            versionMap.put("lxdh",recordSet.getString("lxdh"));
            versionMap.put("jbrName",recordSet.getString("jbrName"));
            versionMap.put("fzrName",recordSet.getString("fzrName"));
            versionMap.put("sbdw",recordSet.getString("sbdw"));
            versionMap.put("sbks",recordSet.getString("sbks"));
            versionMap.put("dlhsdw",recordSet.getString("dlhsdw"));
            String sydw = recordSet.getString("sydw");
            StringBuilder sb = new StringBuilder();
            String[] split = sydw.split(",");
            for (String s : split) {
                String sql = "select subcompanyname from HrmSubCompany where id = ?";
                Map<String, Object> mapBySql = DaoUtil.getMapBySql(sql, s);
                String subcompanyname = Util.null2String(mapBySql.get("subcompanyname"));
                if(!sb.toString().isEmpty()){
                    sb.append(",");
                    sb.append(subcompanyname);
                }else{
                    sb.append(subcompanyname);
                }
            }
            versionMap.put("sydw",sb.toString());
        }
        map.put("jinfeiVersionList",versionMap);

        int number = 0;
        String jinfeiSql = "select  ROW_NUMBER() OVER (order by lcbj)as RowNumber,lcbj,lcjlid,t1.bmzcjjfl,t2.kmc as bmflmc,t1.zfjjfl,t3.kmc as zfflmc,ze,syed,dj,ybx from uf_jingfei t1\n" +
                "left join uf_jjzcflkmk t2\n" +
                "on t1.bmzcjjfl = t2.kdm\n" +
                "left join uf_jjzcflkmk t3\n" +
                "on t1.zfjjfl = t3.kdm\n" +
                "where lcjlid= ? ";
        List<Map<String, Object>> listMapBySql = DaoUtil.getListMapBySql(jinfeiSql, id);
        map.put("jinfeiList",listMapBySql);


        String snjz = "select yslylx,je,syed,srlx,zcqd,dfczbkxm,zyczbksr,\n" +
                "(select mc from uf_srysfl where id=zcqd)as zcqdmc,\n" +
                "(select xmmc from uf_fzysrxm where id=dfczbkxm)as dfczbkxmmc\n" +
                "from uf_jingfeilaiyuan where yslylx = '上年结转核批' and lcid= ? ";
        recordSet = new RecordSet();
        recordSet.executeQuery(snjz,id);
        List snjzList = new ArrayList();
        number=0;
        while (recordSet.next()){
            Map<String,Object> hply = new HashMap<>();
            hply.put("RowNumber",++number);
            hply.put("yslylx",recordSet.getString("yslylx"));
            hply.put("je",recordSet.getString("je"));
            hply.put("syed",recordSet.getString("syed"));
            hply.put("srlx",recordSet.getString("srlx").equals("0")?"财政拨款":"非财政拨款");
            hply.put("zcqd",recordSet.getString("zcqd"));
            hply.put("fzysrfl",recordSet.getString("zcqdmc"));
            hply.put("dfczbkxm",recordSet.getString("dfczbkxm"));
            hply.put("zyczbksr",recordSet.getString("zyczbksr"));
            hply.put("dfczbkxmmc",recordSet.getString("dfczbkxmmc"));
            hply.put("yslymc",recordSet.getString("srlx").equals("0")
                    ?getZyczbksrName(recordSet.getString("zyczbksr"))
                    :recordSet.getString("zcqdmc")+"/"+recordSet.getString("dfczbkxmmc"));
            snjzList.add(hply);
        }
        map.put("snjzList",snjzList);


        String dnysSql = "select yslylx,je,syed,srlx,zcqd,dfczbkxm,zyczbksr,\n" +
                "(select mc from uf_srysfl where id=zcqd)as zcqdmc,\n" +
                "(select xmmc from  uf_fzysrxm where id=dfczbkxm)as dfczbkxmmc\n" +
                "from uf_jingfeilaiyuan where yslylx = '当年预算核批' and lcid= ? ";
        recordSet = new RecordSet();
        recordSet.executeQuery(dnysSql,id);
        List dnysList = new ArrayList();
        number=0;
        while (recordSet.next()){
            Map<String,Object> hply = new HashMap<>();
            hply.put("RowNumber",++number);
            hply.put("yslylx",recordSet.getString("yslylx"));
            hply.put("je",recordSet.getString("je"));
            hply.put("syed",recordSet.getString("syed"));
            hply.put("srlx",recordSet.getString("srlx").equals("0")?"财政拨款":"非财政拨款");
            hply.put("zcqd",recordSet.getString("zcqd"));
            hply.put("fzysrfl",recordSet.getString("zcqdmc"));
            hply.put("dfczbkxm",recordSet.getString("dfczbkxm"));
            hply.put("zyczbksr",recordSet.getString("zyczbksr"));
            hply.put("dfczbkxmmc",recordSet.getString("dfczbkxmmc"));
            hply.put("yslymc",recordSet.getString("srlx").equals("0")
                    ?getZyczbksrName(recordSet.getString("zyczbksr"))
                    :recordSet.getString("zcqdmc")+"/"+recordSet.getString("dfczbkxmmc"));
            dnysList.add(hply);
        }
        map.put("dnysList",dnysList);

        return JSONUtil.toJsonStr(map);
    }

    /**
     * 获取中央财政拨款收入层级名称
     * @param zcczbksr
     * @return
     */
    public String getZyczbksrName(String zcczbksr){
        if("".equals(zcczbksr) || zcczbksr == ""){
            return "";
        }
        String name = "";
        RecordSet rs = new RecordSet();
        String kmj = new BaseBean().getPropValue("Verification","yj");
        if(zcczbksr.split("_")[0].equals(kmj)){
            rs.execute("select kmdm from uf_zyczbksr_km where id="+zcczbksr.split("_")[1]);
            rs.next();
            name =  rs.getString("kmdm");
        }else{
            rs.execute("select kmdm from uf_zyczbksr_km where id=(select szgnkm from uf_zyczbksr_xj where id="+zcczbksr.split("_")[1]+")");
            rs.next();
            name+=rs.getString("kmdm");
            RecordSet rs2 = new RecordSet();
            List<String> superiorId = getSuperiorId(zcczbksr.split("_")[1]);
            rs2.execute("select srzx from uf_zyczbksr_xj where id in("+ StringUtils.join(superiorId.toArray(),",")+") ");
            while (rs2.next()){
                name+="/"+rs2.getString("srzx");
            }
        }
        return name;
    }

    /**
     * 根据id获取中央下级收入的所有上级id 包含本身
     * @param id
     * @return
     */
    public List<String> getSuperiorId(String id){
        List<String> list = new ArrayList<>();
        RecordSet rs = new RecordSet();
        String sql = " select sjzxsr from  uf_zyczbksr_xj where id = "+id+" ";
        rs.executeQuery(sql);
        rs.next();
        if (StrUtil.isNotEmpty(rs.getString("sjzxsr"))){
            List<String> sjzxsr = getSuperiorId(rs.getInt("sjzxsr")+"");
            list.addAll(sjzxsr);
        }
        list.add(String.valueOf(id));
        return list;
    }
}
